package fun.yao.common.monitor.info;

import fun.yao.common.Constant;
import fun.yao.common.msg.HashtableMsg;
import fun.yao.common.uitls.LogUtil;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * 连接数 信息
 * @author longjinglai
 *
 */
public class ConnNumInfo implements IMonitorInfo {

    private List<ConnNumDetailInfo> alDetail = new ArrayList<ConnNumDetailInfo>();

    public List<ConnNumDetailInfo> getDetails()
    {
        return this.alDetail;
    }

    public ConnNumInfo()
    {

    }

    public ConnNumInfo(HashtableMsg htm)
    {
        for(int i=0;i<htm.size();i++)
        {
            Hashtable<String, String> mapConnNumInfo = htm.get(i);
            if(mapConnNumInfo!=null)
            {
                ConnNumDetailInfo detail = new ConnNumDetailInfo(mapConnNumInfo.get("port"),Long.parseLong(mapConnNumInfo.get("total_num")) , Long.parseLong(mapConnNumInfo.get("curr_num")));
                this.alDetail.add(detail);
            }
        }
    }

    /**
     * 转换为HashtableMsg
     * @return
     */
    public HashtableMsg toHashtableMsg()
    {
        HashtableMsg htm = new HashtableMsg();
        for(int i=0;i<this.alDetail.size();i++)
        {
            Hashtable<String, String> mapConnNumInfo = new Hashtable<String, String>();
            mapConnNumInfo.put("port", this.getDetails().get(i).getPort());
            mapConnNumInfo.put("total_num", this.getDetails().get(i).getTotalNum()+"");
            mapConnNumInfo.put("curr_num", this.getDetails().get(i).getCurrNum()+"");
            htm.add(mapConnNumInfo);
        }
        return htm;
    }

    @Override
    public String toString() {
        return "ConnNumInfo [alDetail=" + alDetail + "]";
    }

    public class ConnNumDetailInfo
    {
        public ConnNumDetailInfo(String port, long totalNum, long currNum)
        {
            this.port = port;
            this.totalNum = totalNum;
            this.currNum = currNum;
        }

        /**端口*/
        private String port = "";

        /**总连接数*/
        private long totalNum = 0;

        /**当前连接数*/
        private long currNum = 0;

        public String getPort() {
            return port;
        }

        public void setPort(String port) {
            this.port = port;
        }

        public long getTotalNum() {
            return totalNum;
        }

        public void setTotalNum(long totalNum) {
            this.totalNum = totalNum;
        }

        public long getCurrNum() {
            return currNum;
        }

        public void setCurrNum(long currNum) {
            this.currNum = currNum;
        }

        @Override
        public String toString() {
            return "ConnNumDetailInfo [port=" + port + ", totalNum=" + totalNum
                    + ", currNum=" + currNum + "]";
        }

    }

    public void print(StringBuilder sbOut) {
        try
        {
            for(int i=0;i<this.getDetails().size();i++)
            {
                ConnNumDetailInfo detailInfo = this.getDetails().get(i);
                sbOut.append("第" + i + "个端口["+detailInfo.getPort()+"]连接信息\n");
                sbOut.append("总连接数："+detailInfo.getTotalNum()+"，当前连接数："+detailInfo.getCurrNum()+"\n");
            }
        }catch (Exception e) {
            LogUtil.WriteLog(Constant.BADLY_ERROR, e);
        }
    }
}

